<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-07-05
 * Time: 20:09
 */

namespace App\Utils;

use DB;
use Illuminate\Http\Request;



trait CommonCRUD
{

    protected function _getViewDir() {
        $view= property_exists($this, '_view')?$this->_view:'';
        $view= property_exists($this, '_viewGroup') && $this->_viewGroup?$this->_viewGroup.'.'.$view:$view;
        return $view;
    }

    public function index(Request $request) {
        $model= property_exists($this, '_model')?$this->_model:null;
        $view= $this->_getViewDir();

        $where = property_exists($this, '_where')?$this->_where:[];
        $pageSize = property_exists($this, 'pageSize')?$this->pageSize:25;
        $orderField = property_exists($this, '_orderField')?$this->_orderField:'id';
        $order = property_exists($this, '_order')?$this->_order:'desc';
        if($model) {
            $list = $model::where($where)
                            ->orderBy($orderField, $order)
                            ->paginate($pageSize);

            $data = [
                'list'=>$list
            ];
            if(method_exists($this,'_before_index')) {
                $custom_data = $this->_before_index($request,$list);
                if($custom_data)
                    $data = array_merge($data,$custom_data);
            }

            session()->put('index.url',url()->full());
            return view($view.".index",$data);
        }
    }

    public function add(Request $request) {

        if($request->method()=='GET') {
            $data = [];
            
            if(method_exists($this,'_before_add')) {
                $data = $this->_before_add($request);

            }

            $view= $this->_getViewDir();

            return view($view.".add",$data);
        } else {
            return $this->postAdd($request);
        }
    }

    protected function postAdd(Request $request) {
        $model= property_exists($this, '_model')?$this->_model:null;
        if($model) {
            $data = $request->only($model->getFillable());
            $data = array_where($data,function($val,$key){
                return $val;
            });

            if(method_exists($this,'_before_postAdd')) {
                $custom_data = $this->_before_postAdd($request);
                if( $custom_data ) {
                    if(is_array($custom_data))
                        $data = array_merge($data,$custom_data);
                    else
                        return $custom_data;
                }
                
            }
            
            $row = $model::create($data);
            if($row) {

                if(method_exists($this,'_after_postAdd')) {
                    $after_rt = $this->_after_postAdd($row);
                    if($after_rt)
                        return $after_rt;
                }

                if(property_exists($this, '_redirectUrl')) {
                    $url = $this->_redirectUrl;
                } else {
                    $url = session('index.url');
                }
                return redirect($url)
                    ->with('msg','添加成功！');
            } else {
                return redirect()->back()
                    ->withErrors([
                        '新增失败！'
                    ])->withInput();
            }
        }
    }

    public function edit(Request $request) {
        $id = $request->input('id');
        $model= property_exists($this, '_model')?$this->_model:null;
        $view= $this->_getViewDir();

        if($model) {
            if($request->method()=='GET') {
                
                $vo = $model::findOrFail($id);
                $data = [
                    'vo'=>$vo
                ];
                if(method_exists($this,'_before_edit')) {
                    $custom_data = $this->_before_edit($request,$vo);
                    if($custom_data)
                        $data = array_merge($data,$custom_data);
                }
                return view($view.".edit",$data);
            } else {
                return $this->postEdit($request,$id);
            }
        }
    }

    protected function postEdit(Request $request,$id) {
        $model= property_exists($this, '_model')?$this->_model:null;
        if($model) {
            $data = $request->only($model->getFillable());
            $data = array_where($data,function($val,$key){
                return $val;
            });

            if(method_exists($this,'_before_postEdit')) {
                $vo = $model::findOrFail($id);
                $custom_data = $this->_before_postEdit($request,$vo);
                if( $custom_data ) {
                    if(is_array($custom_data))
                        $data = array_merge($data,$custom_data);
                    else
                        return $custom_data;
                }
            }


            $rt = $model::where('id',$id)->update($data);
            if($rt>=0) {
                if(property_exists($this, '_redirectUrl')) {
                    $url = $this->_redirectUrl;
                } else {
                    $url = session('index.url');
                }
                return redirect($url)
                    ->with('msg','编辑成功！');
            } else {
                return redirect()->back()
                    ->withErrors([
                        '保存失败！'
                    ])->withInput();
            }
        }
    }

    public function del(Request $request) {
        $id = $request->input('id');
        $model= property_exists($this, '_model')?$this->_model:null;
        if($model) {

            $vo = $model::find($id);
            if(method_exists($this,'_before_del')) {
                $rt = $this->_before_del($request,$vo);
                if($rt) {
                    return redirect()->back()
                        ->withErrors([$rt]);
                }
            }

            return DB::transaction(function() use($model,$id,$vo,$request) {
                $rt = $model::where('id',$id)->delete();
                if($rt) {

                    if(method_exists($this,'_after_del')) {
                        $after_rt = $this->_after_del($request,$vo);
                        if($after_rt)
                            return $after_rt;
                    }

                    if(property_exists($this, '_redirectUrl')) {
                        $url = $this->_redirectUrl;
                    } else {
                        $url = session('index.url');
                    }
                    return redirect($url)
                        ->with('msg','删除成功！');
                } else {
                    return redirect()->back()
                        ->withErrors([
                            '删除失败！'
                        ]);
                }
            });

        }
    }




}